package com.cyz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cyz.pojo.User;
import com.cyz.mapper.PlaceMapper;
import com.cyz.pojo.Paging;
import com.cyz.pojo.Place;
import com.cyz.pojo.Type;
import com.cyz.service.PlaceService;
import com.cyz.service.SessionService;
import com.cyz.service.SubjectService;
import com.cyz.service.TypeService;
import com.cyz.service.UserService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/Subject")
public class Subject {

	@Resource(name="gson")
	private Gson gson;
	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="userServiceImpl")
	private UserService userService;
	@Resource(name="typeServiceImpl")
	private TypeService typeService;
	@Resource(name="placeServiceImpl")
	private PlaceService placeService;
	@Resource(name="sessionServiceImpl")
	private SessionService sessionService;
	@Resource(name="subjectServiceImpl")
	private SubjectService subjectService;
	/**
	 * 考试预约
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/subject")
	public ModelAndView reserve(HttpServletRequest request,HttpServletResponse response) {
		mv.setViewName("subject");
		String idNumber=(String)request.getSession().getAttribute("idNumber");
		//System.out.println(idNumber);
		User user=userService.getUserByIdNumber(idNumber);
		Integer typeId=user.getTypeId();
		//获取驾驶证类型
		Type type=typeService.getTypeById(typeId);
		mv.addObject("user", user);
		mv.addObject("typeName",type.getTypeName());
	
		return mv;
	}
	/**
	 * 验证这个考生可预约的科目
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/status")
    public void status(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
		PrintWriter printWriter =response.getWriter();
		mv.setViewName("status");
		Integer status = Integer.parseInt(request.getParameter("subject"));
		//System.out.println(status);
		String idNumber=(String)request.getSession().getAttribute("idNumber");
		User user=userService.getUserByIdNumber(idNumber);
		
		if(user.getStatus()==status)
		{
			
			 printWriter.write("{\"res\":\"1\"}");
		}
		else {
			 printWriter.write("{\"res\":\"0\"}");
		}
		request.getSession().setAttribute("status",status);
	//	System.out.println("status:"+status);//科目测试
		 printWriter.close();
		
    }
	@RequestMapping("/place")
	public void place(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		String idNumber=(String)request.getSession().getAttribute("idNumber");
		String address1=request.getParameter("address1");
	//	System.out.println("address:"+address1);
		request.getSession().setAttribute("address1",address1);
		User user=userService.getUserByIdNumber("1");
		List<Place> list = placeService.getPlaceByArea(user.getArea());
		String s = gson.toJson(list);
		//System.out.println(s);
		printWriter.write(s);
		printWriter.close();
	}
	@RequestMapping("/read")
	public void read(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String place=request.getParameter("address1");
	//	System.out.println(place);
		PrintWriter printWriter = response.getWriter();
		request.getSession().setAttribute("place", place);	
		//System.out.println("place:"+request.getSession().getAttribute("place"));
		//System.out.println("status:"+request.getSession().getAttribute("status"));
		//System.out.println("idNumber:"+request.getSession().getAttribute("idNumber"));
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
		
	}
	@RequestMapping("/time")
	public void time(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
	
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		Map map = new HashMap();
		Integer status =  (Integer)request.getSession().getAttribute("status");
		com.cyz.pojo.Subject subject =subjectService.getSubjectById(status);
		map.put("page",Integer.parseInt(request.getParameter("page")));
		map.put("pageSize",Integer.parseInt(request.getParameter("pageSize")));
		///获取类型名字
		String idNumber=(String)request.getSession().getAttribute("idNumber");
		//System.out.println(idNumber);
		User user=userService.getUserByIdNumber(idNumber);
		Integer typeId=user.getTypeId();
		//获取驾驶证类型
		Type type=typeService.getTypeById(typeId);
	
        map.put("typeName",type.getTypeName());
		//数据库没有typename行
		
		map.put("subject",subject.getSubjectName());

		map.put("startTime",request.getParameter("startTime"));//ok
		
		map.put("endTime",request.getParameter("endTime"));//ok
		map.put("state",1);
		//map.put("state",Integer.parseInt(request.getParameter("state")));
		/**********/
		map.put("place",request.getSession().getAttribute("place"));//ok
	//	map.put("status")
		Paging paging = sessionService.getSession(map);
		String s = gson.toJson(paging);
		//System.out.println(s);
		printWriter.write(s);
		printWriter.close();	
	}
	
}
